package com.mdd.front.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.GreetingCardsRecord;
import com.mdd.common.entity.GreetingCardsSet;
import com.mdd.common.util.StringUtils;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.IGreetingCardsService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.vo.greetingCards.WishVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/greeting")
@Api(tags = "贺卡相关API")
public class GreetingCardsController {
	
	@Autowired
	private IGreetingCardsService greetingCardsService;
	
	@GetMapping("/greetingCardList")
	@ApiOperation(value = "贺卡列表")
	public AjaxResult<List<GreetingCardsSet>> greetingCardList() {
		List<GreetingCardsSet> greetingCardList = greetingCardsService.greetingCardList();
		return AjaxResult.success(greetingCardList); 
	}
	
	@PostMapping("/giveGreetingCards")
	@ApiOperation(value = "赠送贺卡")
	public AjaxResult<Object> giveGreetingCards(@RequestBody GreetingCardsRecord card) {
		if(null == card || StringUtils.isEmpty(card.getContent())) {
			return AjaxResult.failed("请输入祝福语");
		}
		if(null == card.getToUserId()) {
			return AjaxResult.failed("请选择用户");
		}
		if(null == card.getCardId()) {
			return AjaxResult.failed("请选择贺卡");
		}
		Integer userId = LikeFrontThreadLocal.getUserId();
		if(userId == card.getToUserId()) {
			return AjaxResult.failed("自己不可赠送给自己");
		}
		return greetingCardsService.giveGreetingCards(userId,card);
	}
	
	@GetMapping("/wishList")
	@ApiOperation(value = "心愿列表")
	public AjaxResult<PageResult<WishVo>> wishList(Integer type,Integer toUserId,@Validated PageValidate pageValidate) {
		if(null == type) {
			type = 0;
		}
		Assert.isTrue(!(null == toUserId || toUserId <= 0), "请选择用户");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return AjaxResult.success(greetingCardsService.wishList(userId, type, toUserId, pageValidate)); 
	}
}
